$(function() {
	
		$("#dialog").dialog("destroy");
		
		getCunrent();
		
	    var name = $("#name"), allFields = $( []).add(name), tips = $(".validateTips");

		/*
		function updateTips(t) {
			tips.text(t).addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o, n, min, max) {
			var dtDealingType1 = $.trim(o.val());
			var cArr1 = dtDealingType1.match(/[^x00-xff]/ig);
			var the_length = dtDealingType1.length
					+ (cArr1 == null ? 0 : cArr1.length);
			if (the_length > max || the_length < min) {
				o.addClass('ui-state-error');
				updateTips(n + "长度必须在 " + min + " 和 " + (max/2) + "之间.");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o, regexp, n) {
			if (!(regexp.test(o.val()))) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		*/
	
	
		//修改
		$("#dialog-form-modify").dialog( { 
					autoOpen : false,
					height : 350,
					width : 350,
					modal : true,
					buttons : {
						'提交' : function() {
							//name = $("textarea[name='name_modify']");
							//var bValid = true;			
							//allFields.removeClass('ui-state-error');
							//bValid = bValid && checkLength(name, "名字", 1, 30);						
							//if (bValid) {
								//$("#confirmUI_modify").dialog('open');
							//}
							var psId = $("input[name='id_modify']").val();
							var psName = $("textarea[name='name_modify']").val();		
							var psContent = $("textarea[name='content_modify']").val();	
							var psLevel = $('#psLevel_modify option:selected').attr('value');
							//alert(psId+","+psName+","+psContent);
							if(psName=="" && psContent ==""){
								alert("目录和内容都不能为空");
								return;
							}
							var req = {
									"planStandard.elId" :psId,
									"planStandard.EEntry" : psName,
									"planStandard.PContent":psContent,
									"planStandard.PLevel":psLevel
							};
							var url = "planStandard_update.action";
							$.post(url, req, function(data) { // 传入到action
										$("#dialog-form-modify").dialog('close');
										var message ="";
										if(data.ajaxStr="1"){
											message="修改成功！"
										}else{
											message="修改失败！"
										}
										result_alert(message);
									});
						},
						'取消' : function() {
							$(this).dialog('close');
						}
					},
					close : function() {
						//$("#dialog-form-modify").dialog('close');
					}
				});

		/*
		$("#confirmUI_modify").dialog( { // 修改时的confirm
					autoOpen : false,
					height : 100,
					width : 350,
					modal : true,
					buttons : {
						'修改' : function() {
							$("button[name='修改']").attr("disabled", "disabled");
							allFields.removeClass('ui-state-error');
							var psId = $("input[name='id_modify']").val();
							var psName = $("textarea[name='name_modify']").val();		
							var psContent = $("textarea[name='content_modify']").val();	
							alert(psId+","+psName+","+psContent);
							var req = {
									"planStandard.elId" :psId,
									"planStandard.EEntry" : psName,
									"planStandard.PContent" : psContent
							};
							var url = "planStandard_update.action";
							$.post(url, req, function(data) { // 传入到action
										alert("updateok");
										$("#confirmUI_modify").dialog('close');
										//$("#dialog-form-modify").dialog('close');
										//getCunrent();
										result_alert(data);
									});
						},
						'取消' : function() {
							$(this).dialog('close');
						}
					},
					close : function() {
						allFields.val('').removeClass('ui-state-error');
						
					}
				});
*/
		// 删除form
		$("#confirmUI_delete").dialog( { 
			autoOpen : false,
			height : 200,
			width : 350,
			modal : true,
			buttons : {
				'删除' : function() {
					$("button[name='删除']").attr("disabled", "disabled");
					allFields.removeClass('ui-state-error');
					
					/* 这里提交 */
					var psId = $("input[name='id_delete']").val();					
					var data = {
							psID : psId
					};
					var url = "planStandard_delete.action";
					$.post(url, data, function(data) { // 传入到action
								$("#confirmUI_delete").dialog('close');
								var message ="";
								if(data.ajaxStr="1"){
									message="删除成功！"
								}else{
									message="删除失败！"
								}
								result_alert(message);
							});
					/* 提交结束 */
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			},
			close : function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		 
		function result_alert(message) {
			$("div[name='alert']").text(message).dialog('open');
		}
		
		$("div[name='alert']").dialog( {
			autoOpen : false,
			height : 100,
			width : 350,
			modal : true,
			buttons : {
				'确定' : function() {
					window.location.replace("/ag/hmk/findPlanStandard.jsp");
				}
			},
			close : function() {
				window.location.replace("/ag/hmk/findPlanStandard.jsp");
			}
		});
			
			
		//新增
		$('#create-user').click(function() {
			$('#dialog-form').dialog('open');
		});
		
		$("#dialog-form").dialog( {                 // 增加form的情况
					autoOpen : false,
					height : 350,
					width : 350,
					modal : true,
					buttons : {
						'保存' : function() {
							/*
							name = $("textarea[name='name_add']");	
							var content = $("textarea[name='content_add']");
							alert(name+","+content);							
							var bValid = true;
							allFields.removeClass('ui-state-error');
							bValid = bValid && checkLength(name, "名字", 1, 30);							
							if (bValid){
								$("#confirmUI_add").dialog('open');
							}
							*/
							var psName = $("textarea[name='name_add']").val();	
							var psContent = $("textarea[name='content_add']").val();
							var psLevel = $('#psLevel_add option:selected').attr('value');
							if(psName=="" && psContent ==""){
								alert("目录和内容都不能为空");
								return;
							}					
							var data = {					
									"planStandard.EEntry" : psName,
									"planStandard.PContent":psContent,		
									"planStandard.PLevel":psLevel			
							};
							var url = "planStandard_save.action";
						
							$.post(url, data, function(data) { // 传入到action
										$("#dialog-form").dialog('close');
										var message ="";
										if(data.ajaxStr="1"){
											message="保存成功！"
										}else{
											message="保存失败！"
										}
										result_alert(message);
										
									});
						},
						'取消' : function() {
							$(this).dialog('close');
						}
					},
					close : function() {
						allFields.val('').removeClass('ui-state-error');
						$("#content_add").val("");
					}
				});

	/*
		$("#confirmUI_add").dialog( {                // 增加confirm
					autoOpen : false,
					height : 100,
					width : 350,
					modal : true,
					buttons : {
						'增加' : function() {
							$("button[name='增加']").attr("disabled", "disabled");
							allFields.removeClass('ui-state-error');

							var psName = $("textarea[name='name_add']").val();	
							var psContent = $("textarea[name='content_add']").val();
							alert(psName+","+psContent);						
							var data = {					
									"planStandard.EEntry" : psName,
									"planStandard.PContent":psContent					
							};
							var url = "planStandard_save.action";
						
							$.post(url, data, function(data) { // 传入到action
										alert("ff");
										$("#dialog-form").dialog('close');
										$("#confirmUI_add").dialog('close');
										//$("#confirmUI_add").dialog('open');
										getCunrent();
										//result_alert(data);
										
									});
							
						},
						'取消' : function() {
							$(this).dialog('close');
							
						}
					},
					close : function() {
						allFields.val('').removeClass('ui-state-error');
					}
				});
*/

/*		$("tr.list").mouseover(function() {                                 //行跟着鼠标闪效果
			$(this).children().css("background-color", "#66FFFF");
		}).mouseout(function() {
			$(this).children().css("background-color", "white");
		});
*/
		
		
	function getCunrent(){
		$('#PSbodyID').empty();
		$.ajax({
  			type: "post",
 			url: "planStandard_findALL.action",
  			success:showData
		});
	}

	function showData(data){
		$.each(data.planstandardList,function(i,n){
  				fillDataToHtml(n.elId,n.EEntry,n.PContent,n.PLevel);
		});
	}
	
	
	function fillDataToHtml(psid,psName,psContent,psLevel){
		var strHtml="<tr class='list'>";
		strHtml+="<td height='18' bgcolor='#FFFFFF' class='id_' style='display: none'>";
        strHtml+=psid+"</td>";
        strHtml+="<td height='18' bgcolor='#FFFFFF' class='name_' align='center'>";
        strHtml+=psName+"</td>";
        strHtml+="<td height='70px' bgcolor='#FFFFFF' class='content_' align='center'>";
        strHtml+="<div style='height:100%;width:screen.width * 0.2 px;overflow-y:auto;overflow-x:auto;word-wrap:break-word;text-align:left;'>"+psContent+"</div></td>";
        strHtml+="<td height='18' bgcolor='#FFFFFF' align='center'>";
        strHtml+=psLevel+"</td>"
        strHtml+="<td height='18' bgcolor='#FFFFFF' align='center'>";
        strHtml+="<button style='width: 50px; height: 25px; border: 0px none; background: url(&quot;/ag/zrt/images/modify_mouseout.gif&quot;) repeat scroll 0% 0% transparent;' class='modify' ";
        strHtml+=" onclick='changePS(this)'></button>";
        strHtml+="</td>";
        strHtml+="<td height='18' bgcolor='#FFFFFF' align='center'>";
		strHtml+="<button style='width: 50px; height: 25px; border: 0px none; background: url(&quot;/ag/zrt/images/delete_mouseout.gif&quot;) repeat scroll 0% 0% transparent;' class='delete' name='0001' " ; //></button>";
		strHtml+=" onclick='deletePS(this)'></button>";
        strHtml+="</td>";
        strHtml+="</tr>";
        
        $('#PSbodyID').append(strHtml);
	}
	
});


	function deletePS(v){
			$('#confirmUI_delete').dialog('open');
			var Id_val = $.trim($(v).parent().siblings(".id_").text()); // 取兄弟即id的内容放到弹出form			
			var name_val = $.trim($(v).parent().siblings(".name_")
					.text());
			$("input[name='id_delete']").val(Id_val);
			$("#deletNameID").text(name_val);
	}
	
	function changePS(v){
			var id_val = $.trim($(v).parent().siblings(".id_").text()); // 取兄弟即id的内容放到弹出form
			var name_val = $.trim($(v).parent().siblings(".name_")
					.text());
			var content_val = $.trim($(v).parent().siblings(".content_")
					.text());
			$("textarea[name='name_modify']").val(name_val);
			$("textarea[name='content_modify']").val(content_val);
			$("input[name='id_modify']").val(id_val);
			$('#dialog-form-modify').dialog('open');
	}
	
	